package com.smcdd.cdb.dbo.cdb_dbo.ui.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.GlideRoundTransform;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.SecurityCodeView;

/**
 * 作者：Administrator on 2017/7/17 17:07
 * 邮箱：zhanghuaiha@gmail.com
 */

public class DialogUtils {

    /**
     * 提示dialog
     * @param context 上下文
     * @param message 加载信息
     * @param flag  点击红白处是否消失，true为消失，false为不可消失
     */
    public static void TSDialog(Context context,String message,boolean flag){
        Dialog progressDialog = new Dialog(context, R.style.progress_dialog);
        progressDialog.setContentView(R.layout.dialog_ts);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(flag);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView msg = (TextView) progressDialog.findViewById(R.id.id_tv_loadingmsg);
        TextView save = (TextView) progressDialog.findViewById(R.id.dialog_save);
        save.setOnClickListener(v -> {
            progressDialog.dismiss();
        });
        msg.setText(message);
        progressDialog.show();

    }

    /**
     * 二维码选着dialog
     * @param context 上下文
     */
    public static Dialog CodeDialog(Context context){
        Dialog progressDialog = new Dialog(context, R.style.progress_dialog);
        progressDialog.setContentView(R.layout.dialog_code);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        return progressDialog;
    }


    /**
     * 二维码dialog
     * @param context 上下文
     * @param name  商家账户
     */
    public static void TSImageDialog(Context context, String name,String aid,String url, Bitmap bitmap){
        Dialog progressDialog = new Dialog(context, R.style.progress_dialog);
        progressDialog.setContentView(R.layout.dialog_image);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(true);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView user = (TextView) progressDialog.findViewById(R.id.dialog_user);
        TextView id = (TextView) progressDialog.findViewById(R.id.dialog_id);
        ImageView er = (ImageView) progressDialog.findViewById(R.id.dialog_image);
        ImageView icon = (ImageView) progressDialog.findViewById(R.id.dialog_icon);
        er.setImageBitmap(bitmap);
        user.setText(name);
        id.setText(aid);
        RequestOptions myOptions = new RequestOptions()
                .transform(new GlideRoundTransform(context,10));
        Glide.with(context)
                .asBitmap()
                .load(url)
                .apply(myOptions)
                .into(icon);
        progressDialog.show();

    }


    /**
     * 提示dialog
     * @param context 上下文
     * @param message 加载信息
     * @param flag  点击红白处是否消失，true为消失，false为不可消失
     */
    public static void TSLeftDialog(Context context,String message,boolean flag){
        Dialog progressDialog = new Dialog(context, R.style.progress_dialog);
        progressDialog.setContentView(R.layout.dialog_ts_left);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(flag);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView msg = (TextView) progressDialog.findViewById(R.id.id_tv_loadingmsg);
        TextView save = (TextView) progressDialog.findViewById(R.id.dialog_save);
        save.setOnClickListener(v -> {
            progressDialog.dismiss();
        });
        msg.setText(message);
        progressDialog.show();

    }


    /**
     * 更新dialog
     * @param context 上下文
     * @param message 加载信息
     * @param flag  点击红白处是否消失，true为消失，false为不可消失
     */
    public static void UpDialog(Context context,String message,boolean flag,String url){
        Dialog progressDialog = new Dialog(context, R.style.progress_dialog);
        progressDialog.setContentView(R.layout.dialog_up);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(flag);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView msg = (TextView) progressDialog.findViewById(R.id.id_tv_loadingmsg);
        TextView save = (TextView) progressDialog.findViewById(R.id.dialog_save);
        TextView cancel = (TextView) progressDialog.findViewById(R.id.dialog_cancel);
        TextView title = (TextView) progressDialog.findViewById(R.id.dialog_title);
        title.setText("发现新版本");
        cancel.setOnClickListener(v -> {
            progressDialog.dismiss();
        });
        save.setOnClickListener(v -> {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(url);
                intent.setData(content_url);
                context.startActivity(intent);
            progressDialog.dismiss();
        });
        msg.setText(message);
        progressDialog.show();
    }


    /**
     * 提示dialog
     * @param context 上下文
     * @param message 加载信息
     * @param flag  点击红白处是否消失，true为消失，false为不可消失
     */
    public static void UpDialogUrl(Context context,String message,boolean flag,String url){
        Dialog progressDialog = new Dialog(context, R.style.progress_dialog);
        progressDialog.setContentView(R.layout.dialog_ts);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(flag);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView msg = (TextView) progressDialog.findViewById(R.id.id_tv_loadingmsg);
        TextView save = (TextView) progressDialog.findViewById(R.id.dialog_save);
        TextView title = (TextView) progressDialog.findViewById(R.id.dialog_title);
        title.setText("发现新版");
        save.setText("更新");
        save.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(url);
            intent.setData(content_url);
            context.startActivity(intent);
            progressDialog.dismiss();
        });
        msg.setText(message);
        progressDialog.show();

    }



    /**
     * 加载dialog
     * @param context 上下文
     * @param message 加载信息
     * @param flag  点击红白处是否消失，true为消失，false为不可消失
     */
    public static Dialog uploadDialog(Context context,String message,boolean flag){
        Dialog progressDialog = new Dialog(context, R.style.progress_dialog);
        progressDialog.setContentView(R.layout.dialog);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(flag);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView msg = (TextView) progressDialog.findViewById(R.id.id_tv_loadingmsg);
        msg.setText(message);
        progressDialog.show();
        return progressDialog;
    }


    /**
     * 提示dialog
     * @param context 上下文
     * @param message 加载信息
     * @param flag  点击红白处是否消失，true为消失，false为不可消失
     */
    public static Dialog ClickDialog(Context context,String message,boolean flag){
        Dialog progressDialog = new Dialog(context, R.style.progress_dialog);
        progressDialog.setContentView(R.layout.dialog_up);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(flag);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView msg = (TextView) progressDialog.findViewById(R.id.id_tv_loadingmsg);
        TextView save = (TextView) progressDialog.findViewById(R.id.dialog_save);
        TextView cancel = (TextView) progressDialog.findViewById(R.id.dialog_cancel);
        cancel.setVisibility(View.GONE);
        save.setOnClickListener(v -> {
            progressDialog.dismiss();
        });
        msg.setText(message);
        progressDialog.show();
        return progressDialog;
    }

    public static Dialog inputDialog(Context context,boolean flag) {
        Dialog progressDialog = new Dialog(context, R.style.progress_dialog);
        progressDialog.setContentView(R.layout.dialog_input);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(flag);
        return progressDialog;
    }
}